TOOL.Category = "Constraints"
TOOL.Name = "Unconstraint"
TOOL.Command = nil
TOOL.ConfigName = ""

TOOL.ClientConVar["constraint_type"] = "Weld";

if(CLIENT) then
	language.Add("Tool_unconstraint_name", "Unconstraint");
	language.Add("Tool_unconstraint_desc", "Removes constraints");
	language.Add("Tool_unconstraint_0", "Left click on a prop to remove constraints of the selected type. Right click removes constraints of all types.");
end

function TOOL.BuildCPanel(panel)
	panel:AddControl("Header", { Text = "#Tool_unconstraint_name", Description = "#Tool_unconstraint_desc" });
	panel:AddControl("ComboBox", {
		Label = "Type",
		MenuButton = 0,
		Options = {
			["Axis"] = { unconstraint_constraint_type = "Axis" },
			["Ballsocket"] = { unconstraint_constraint_type = "Ballsocket" },
			["Elastic"] = { unconstraint_constraint_type = "Elastic" },
			["Hydraulic"] = { unconstraint_constraint_type = "Hydraulic" },
			["Keepupright"] = { unconstraint_constraint_type = "Keepupright" },
			["Muscle"] = { unconstraint_constraint_type = "Muscle" },
			["Pulley"] = { unconstraint_constraint_type = "Pulley" },
			["Rope"] = { unconstraint_constraint_type = "Rope" },
			["Slider"] = { unconstraint_constraint_type = "Slider" },
			["Weld"] = { unconstraint_constraint_type = "Weld" },
			["Winch"] = { unconstraint_constraint_type = "Winch" }
		}
	});
end

function TOOL:LeftClick(trace)
	if(isRemoveable(trace)) then
		return constraint.RemoveConstraints(trace.Entity, self:GetClientInfo("constraint_type"));
	end
end

function TOOL:RightClick(trace)
	if(isRemoveable(trace)) then
		return constraint.RemoveAll(trace.Entity);
	end
end

function isRemoveable(trace)
	return SERVER && ValidEntity(trace.Entity) && !trace.Entity:IsPlayer() && util.IsValidPhysicsObject(trace.Entity, trace.PhysicsBone);
end